import Vue from 'vue'
import Vuex from 'vuex'
import qxz from './qxz'
Vue.use(Vuex)
 
export default new Vuex.Store({
	state:{
		tabCard:[],
		tabTitleArr:[],
		tabActive:{},
	},
	mutations:{
		addTabs(state,tab){//添加
			if(state.tabTitleArr.indexOf(tab.name) === -1){
				state.tabTitleArr.push(tab.name);
				state.tabCard.push({
					title: tab.title,
					name: tab.name
				})
			}else{
				
			}
			state.tabActive = tab;   
		
		},
		tabSChecked (state,tab) {
			state.tabActive = tab;
		},
		removeTabs (state,tab){
			var idx = state.tabTitleArr.indexOf(tab.name);
			if(state.tabTitleArr.length === 1){	//1个
				state.tabTitleArr.length = 0;
				state.tabActive = {};
				state.tabCard.length = 0;  
			}else{ //多个
				if(tab.name === state.tabActive.name){ //判断是否avtive的tab
					// state.tabActive = state.tabCard[idx+1];
					if((idx+1) === state.tabTitleArr.length){ // 是否最后一个
						state.tabActive = state.tabCard[0]; //最后一个则第一个active，否则下一个active
					}else{
						state.tabActive = state.tabCard[idx+1];
					}
					
				}
				state.tabCard.splice(idx, 1);
				state.tabTitleArr.splice(idx, 1);

			}
		}
	},
	modules: {
		qxz
	}
})